#include "ParentHandle.h"

using namespace std;

ParentHandle::ParentHandle(void)
{
}

ParentHandle::~ParentHandle(void)
{
}

std::vector<std::vector<INTEGER>> ParentHandle::RetrieveTransitiveParent(
	std::vector<INTEGER> parent_in, std::vector<INTEGER> child_in)
{
	std::sort(child_in.begin(), child_in.end());

	std::vector<std::vector<INTEGER>> result_vector;
	std::vector<INTEGER> parent_out;
	std::vector<INTEGER> child_out;

	STMT parent_stmt;
	parent_stmt.node_type = _NULL;
	unsigned int i;
	for (i = 0; i < parent_in.size(); i++)
	{
		parent_stmt.statement_number = parent_in[i];
		STMT_ARRAY child_temp = PKB::parent.RetrieveTransitiveChild(parent_stmt);

		for(auto it1 = child_temp.begin(); it1 != child_temp.end(); it1++)
		{
			INTEGER stmt_num = it1->statement_number;

			if (std::binary_search(child_in.begin(), child_in.end(), stmt_num))
			{
				parent_out.push_back(parent_in[i]);
				child_out.push_back(stmt_num);
			}
		}
	}

	result_vector.push_back(parent_out);
	result_vector.push_back(child_out);
	
	return result_vector;
}

std::vector<std::vector<INTEGER>> ParentHandle::RetrieveParent(
	std::vector<INTEGER> parent_in, std::vector<INTEGER> child_in)
{
	std::sort(child_in.begin(), child_in.end());

	std::vector<std::vector<INTEGER>> result_vector;
	std::vector<INTEGER> parent_out;
	std::vector<INTEGER> child_out;

	STMT parent_stmt;
	parent_stmt.node_type = _NULL;
	unsigned int i;
	for (i = 0; i < parent_in.size(); i++)
	{
		parent_stmt.statement_number = parent_in[i];
		STMT_ARRAY child_temp = PKB::parent.GetChild(parent_stmt);

		for(auto it1 = child_temp.begin(); it1 != child_temp.end(); it1++)
		{
			INTEGER stmt_num = it1->statement_number;

			if (std::binary_search(child_in.begin(), child_in.end(), stmt_num))
			{
				parent_out.push_back(parent_in[i]);
				child_out.push_back(stmt_num);
			}
		}
	}

	result_vector.push_back(parent_out);
	result_vector.push_back(child_out);
	
	return result_vector;
}